@tool
extends ProgressBar


class_name ColorFlatProgressBar


## 背景颜色
@export var bg_color: Color = Color.GRAY:
    set(v):
        bg_color = v
        var bg: StyleBoxFlat = get("theme_override_styles/background") as StyleBoxFlat
        bg.bg_color = bg_color
## 不同分段的颜色
@export var percent_colors: Array[Color] = []
## 最后填充的颜色
@export var fill_color: Color = Color.GREEN:
    set(v):
        fill_color = v
        var fill: StyleBoxFlat = get("theme_override_styles/fill") as StyleBoxFlat
        fill.bg_color = fill_color
## 边框颜色
@export var bd_color: Color = Color.WHITE:
    set(v):
        bd_color = v
        var bg: StyleBoxFlat = get("theme_override_styles/background") as StyleBoxFlat
        var fill: StyleBoxFlat = get("theme_override_styles/fill") as StyleBoxFlat
        bg.border_color = bd_color
        fill.border_color = bd_color
## 边框宽度
@export var border_width: int = 1:
    set(v):
        border_width = v

        var bg: StyleBoxFlat = get("theme_override_styles/background") as StyleBoxFlat
        var fill: StyleBoxFlat = get("theme_override_styles/fill") as StyleBoxFlat
        __border_width_changed(bg, fill)
        _value_changed(value)
## 圆角半径
@export var radius: int = 1:
    set(v):
        radius = v
        var bg: StyleBoxFlat = get("theme_override_styles/background") as StyleBoxFlat
        var fill: StyleBoxFlat = get("theme_override_styles/fill") as StyleBoxFlat

        __radius_changed(bg, fill)


var border_rate: float = 0


func __border_width_changed(bg: StyleBoxFlat, fill: StyleBoxFlat) -> void:
    bg.border_width_left = border_width
    bg.border_width_right = border_width
    bg.border_width_top = border_width
    bg.border_width_bottom = border_width

    fill.border_width_left = border_width
    fill.border_width_right = 0 if value < max_value else border_width
    fill.border_width_right = border_width
    fill.border_width_top = border_width
    fill.border_width_bottom = border_width

    border_rate = 1.0 - 2.0 * border_width / size.x

func __radius_changed(bg: StyleBoxFlat, fill: StyleBoxFlat) -> void:
    bg.corner_radius_bottom_left = radius
    bg.corner_radius_bottom_right = radius
    bg.corner_radius_top_left = radius
    bg.corner_radius_top_right = radius

    fill.corner_radius_bottom_left = radius
    fill.corner_radius_bottom_right = radius
    fill.corner_radius_top_left = radius
    fill.corner_radius_top_right = radius

func _ready() -> void:
    var bg: StyleBoxFlat = StyleBoxFlat.new()
    bg.bg_color = bg_color
    bg.border_color = bd_color

    var fill: StyleBoxFlat = StyleBoxFlat.new()
    fill.bg_color = fill_color
    fill.border_color = bd_color

    __border_width_changed(bg, fill)
    __radius_changed(bg, fill)

    set("theme_override_styles/background", bg)
    set("theme_override_styles/fill", fill)
    show_percentage = false

func _value_changed(new_value: float) -> void:
    if border_width > 0:
        var is_hide_border: bool = border_rate >= (new_value / max_value) if border_rate >= step else new_value < max_value
        var fillbox: StyleBoxFlat = get("theme_override_styles/fill") as StyleBoxFlat
        fillbox.border_width_right = 0 if is_hide_border else border_width
    if percent_colors.size() < 1:
        return
    var percent_step: float = max_value / (percent_colors.size() + 1)
    var percent_index: int = int(new_value / percent_step)
    var color: Color = percent_colors[percent_index] if percent_index < percent_colors.size() else fill_color
    var fill: StyleBoxFlat = get("theme_override_styles/fill") as StyleBoxFlat
    fill.bg_color = color
